<?php
/**
 * Created by PhpStorm.
 * User: dwlw
 * Date: 2018/5/7
 * Time: 10:25
 */
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Caser;
use think\Db;

class News extends Base{
    //案件列表
    public function lst(){
        //获取用户信息
        $case = new Caser();
        $casies = $case->table('d_news');

        $title = input('title');
        if (isset($title)) {
            $casies->where('title', 'like', "%{$title}%");
        }

        $resu = $casies->order('id','desc')->paginate(10);
        $page = $resu->render();
        $this->assign('casies', $resu);
        $this->assign('page', $page);
        return $this->fetch();
    }


    //案件添加
    public function add(){
        if ($this->request->isPost()){
            $this->get_add();
        }else{
            return $this->fetch();
        }
    }


    //案件列表
    public function edit(){
        if ($this->request->isPost()){
            $this->get_edit();
        }else{
            $id = input('id');
            if (isset($id) && is_numeric($id)) {
                $case = Db::table('d_news')->find($id);
            }

            if (empty($case)) {
                return $this->error('没有找到该数据');
            }

            $this->assign('case',$case);
            return $this->fetch();
        }

    }


    //处理add提交数据
    public function get_add(){
        $datas = input('post.');
        $datas['updated_at'] = date('Y-m-d H:i:s');
        //重组数组数据
        $cl = new Caser();
        $resu = $cl->table('d_news')->insert($datas);

        if(!$resu){
            $this->error('添加错误');
        }else{
            $this->success('新增成功', 'News/lst');
        }
    }

    public function destroy() {
        $id = input('id');
        if (isset($id) && is_numeric($id)) {
            Db::table('d_news')->delete($id);
        }
        $this->success('删除成功', 'News/lst');
    }


    //处理add提交数据
    public function get_edit(){
        $datas = input('post.');
        $id = $datas['id'];
        unset($datas['id']);

        $flag = true;
        if (isset($id) && is_numeric($id)) {
            Db::table('d_news')->where('id', $id)->update($datas);
            $flag = false;
        }

        if($flag){
            $this->error('编辑失败', 'Users/edit' , ['id'=>$id]);
        }else{
            $this->redirect('/admin/News/edit?id='.$id);
        }
    }


    //添加案件记录
    public function asdd_record(){
        //获取用户信息
        $row = input();
        //1.通过id查询获取完整案件信息
        $case = new Caser();
        $News = $case->get_center($row['id']);
        //重组数组
        $arr = $case->get_cor($row,$News);
        if($arr['recordm']==''){
            $this->error('记录标题不能为空。');
        }else{
            $case->table('d_record')->insert($arr);
            $this->success('添加记录成功', 'News/center?id='.$arr['caseid']);
        }
//        print_r($arr);die;
        return $this->fetch();
    }


    //文件上传
    public function upload($field){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file($field);
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move($_SERVER['DOCUMENT_ROOT'].'/uploads');
            if($info){
                // 成功上传后 获取上传信息
                $urls =  $info->getFilename();
                $url = '/uploads/'.date('Ymd').'/'.$urls;
                return $url;
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }else{
            return 999;
        }
    }

}
